将你的代码上传 Bintray 仓库

概述

在 Android Studio 中,我们通常可以利用 gradle 来导入别人写的第三方库,通常可以简单得使用一句话就能搞定整个导包过程,比如:

1
compile 'net.cpacm.moneytext:moneyview:1.0.0'

在这个过程中,Android Studio 会从 Maven 仓库服务器中下载所对应的包。现在比较通用的两个服务器分别为 Jcenter 和 Maven Central。本文主要讲的就是Jcenter了,Android Studio 默认使用的服务器仓库。

Jcenter

Jcenter 是 bintray.com 所使用的 Maven 仓库。与 Maven Central 相比,jcenter 的速度更快,包含的库更多,UI界面更友好,更容易使用,同时 bintray 还支持将 jcenter 上传到 Maven Central 的功能。

bintray

语法规则

以前使用过 Maven 的可能会比较清楚,导入的语句如何指向仓库中唯一存在的库。

1
2
compile 'net.cpacm.simpleslider:library:1.0.0'
compile 'GROUP_ID:ARTIFACT_ID:VERSION'

其中
GROUP_ID 是指包所在的组,比如我包放在 net.cpacm.simpleslider 中, 那么我的 GROUP_ID 就是 net.cpacm.simpleslider
ARTIFACT_ID 是指工程名字,一般在android中都为library
VERSION 代表使用的包的版本。

Bintray 使用

请科学上网

bintray 支持 githubgoogle+ 第三方直接登录。
登录后进入 Maven 仓库建立要使用的包,同时填入相应的对应信息。
maven
message
message

Gradle 配置

首先确保你要上传的第三方包是一个 library,作为一个项目的 module 存在。

project gradle

在 project gradle 中加入需要的依赖包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.0'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:1.5"

}
}

allprojects {
repositories {
jcenter()
}
}

task clean(type: Delete) {
delete rootProject.buildDir
}

local.properties

在项目的 local.properties 文件里面加入 api-key

1
2
bintray.user=YOUR_BINTRAY_USERNAME
bintray.apikey=YOUR_BINTRAY_API_KEY

在 bintray 网站的个人信息编辑页可以找到。
api key
对了,记得不要把 local.properties 传到 github网站上导致个人信息的泄露。

library gradle

修改 library 的 gradle 信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
apply plugin: 'com.android.library'
// add plugin
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'

version = '1.0.0'

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"

defaultConfig {
minSdkVersion 11
targetSdkVersion 23
versionCode 1
versionName "1.0.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
}

task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
}

task javadoc(type: Javadoc) {
source = android.sourceSets.main.java.srcDirs
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}

task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives javadocJar
archives sourcesJar
}

group = 'net.cpacm.simpleslider'
install {
repositories.mavenInstaller {
pom.project {
packaging 'aar'
groupId 'net.cpacm.simpleslider' //自己定义的组名
artifactId 'library'

name 'simpleslider'
description 'A simple slider allows you to easily use.'
url 'https://github.com/cpacm/SimpleSlider'
inceptionYear '2016'

licenses {
license {
name 'The Apache Software License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
distribution 'repo'
}
}
scm {
connection 'https://github.com/cpacm/SimpleSlider.git'
url 'https://github.com/cpacm/SimpleSlider'

}
developers {
developer {
name 'cpacm'
email 'shenliming@gmail.com'
}
}
}

}
}

// Bintray

//获取local.propertes的信息
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())

bintray {
user = properties.getProperty("bintray.user")
key = properties.getProperty("bintray.apikey")
publish = true
configurations = ['archives']
pkg {
//填入 bintray 上对应的 package 信息
repo = 'maven'
name = 'SimpleSlider'
vcsUrl = 'https://github.com/cpacm/SimpleSlider.git'
websiteUrl = 'https://github.com/cpacm/SimpleSlider'
licenses = ['Apache-2.0']
issueTrackerUrl = 'https://github.com/cpacm/SimpleSlider/issues'
publicDownloadNumbers = true
version {
name = '1.0.0'
desc = 'simple slider release'
vcsTag = '1.0.0'
attributes = ['gradle-plugin': 'com.use.less:com.use.less.gradle:gradle-useless-plugin']
}
}
}

tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}

task findConventions << {
println project.getConvention()
}

安装和上传

在Android Studio Terminal窗口中运行命令

1
2
gradlew install
gradlew bintrayUpload

至此已经打包上传结束,可以回到 bintray 网站看看结果。
jcenter

添加到Jcenter

点击上图的 Add to jcenter 按钮,向管理员申请添加到 jcenter 仓库,一般等个几小时就能通过。
最后在 https://jcenter.bintray.com 找到自己的库就表示成功了。


参考文章

(1)http://inthecheesefactory.com/blog/how-to-upload-library-to-jcenter-maven-central-as-dependency/en

文章作者: cpacm
文章链接: http://www.cpacm.net/2016/06/01/将你的代码上传 Bintray 仓库/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 cpacm
打赏
  • 微信
  • 支付宝

评论